package com.block.merchant.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.alibaba.fastjson.JSONObject;
import com.block.common.constant.AppRspCodeConstant;
import com.block.common.constant.RedisKeyConstant;
import com.block.common.constant.SmsConstant;
import com.block.common.vo.ResponseVo;
import com.block.db.entity.MerchantApplyInfo;
import com.block.db.mapper.MerchantApplyInfoMapper;
import com.block.db.mapper.MerchantBaseInfoMapper;
import com.block.merchant.service.IMerchantApplyInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.block.storage.enums.BizTypeEnum;
import com.block.storage.model.vo.OssInfoVO;
import com.block.storage.service.IStorageService;
import com.block.test.reqbean.merchant.ApplyInfoBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * <p>
 * 商户申请信息 服务实现类
 * </p>
 *
 * @author 
 * @since 2024-06-19
 */
@Service
public class MerchantApplyInfoServiceImpl extends ServiceImpl<MerchantApplyInfoMapper, MerchantApplyInfo> implements IMerchantApplyInfoService {

    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    IMerchantApplyInfoService merchantApplyInfoService;
    @Autowired
    IStorageService storageService;
    @Resource
    MerchantBaseInfoMapper merchantBaseInfoMapper;

    @Override
    public ResponseVo loginIn(String mobile, String verifyCode,String ownerId) {

        String mobileMd5 = SecureUtil.md5(mobile).toLowerCase();

        String codeKey = SmsConstant.getCodeKey(SmsConstant.LOGIN_REGISTER_MERCHANT, mobileMd5);

        String codeKeyValue = stringRedisTemplate.opsForValue().get(codeKey);
//        if (ObjectUtil.isEmpty(codeKeyValue)){
//            return new ResponseVo(AppRspCodeConstant.SYS_EXCEPTION, "验证码无效");
//        }

        String randomCode = RandomUtil.randomString(32);

        JSONObject returnObject = new JSONObject();
        returnObject.put("mobile", mobile);
        returnObject.put("randomCode", randomCode);

        // 返回商务信息
//        List<Map<Integer, String>> businessMap = merchantBaseInfoMapper.selectBusinessInfoMap();
//        returnObject.put("businessInfo", businessMap);

        MerchantApplyInfo merchantApplyInfo = this.baseMapper.getEntityByMobileLock(mobile);
        if (ObjectUtil.isEmpty(merchantApplyInfo)){
            merchantApplyInfo = new MerchantApplyInfo();
            merchantApplyInfo.setMobile(mobile);
            merchantApplyInfo.setDateInfo(LocalDate.now().toString());
            merchantApplyInfo.setOwnerId(Integer.valueOf(ownerId));
            this.save(merchantApplyInfo);
        }

        // 保存toke 信息
        MerchantApplyInfo updateEntity = new MerchantApplyInfo();
        updateEntity.setId(merchantApplyInfo.getId());
        updateEntity.setTokenInfo(randomCode);
        this.updateById(updateEntity);

        return new ResponseVo(AppRspCodeConstant.SUCCESS, returnObject);
    }

    @Override
    public ResponseVo submitApplyInfo(ApplyInfoBean applyInfoBean) {

        MerchantApplyInfo merchantApplyInfo = this.baseMapper.getEntityByToken(applyInfoBean.getRandomCode());
        if (ObjectUtil.isEmpty(merchantApplyInfo)){
            // 生成新的记录
            return new ResponseVo(AppRspCodeConstant.SYS_EXCEPTION, "验证码无效");
        }

        if (!ObjectUtil.equals(Integer.valueOf(0), merchantApplyInfo.getStatus())){
            return new ResponseVo(AppRspCodeConstant.SYS_EXCEPTION, "状态错误，不可更新");
        }

        // 更新商户提交信息
        MerchantApplyInfo updateEntity = new MerchantApplyInfo();
        updateEntity.setId(merchantApplyInfo.getId());

        updateEntity.setRealname(applyInfoBean.getRealname());
        updateEntity.setIdcardHeadUrl(applyInfoBean.getIdcardHeadUrl());
        updateEntity.setIdcardNationUrl(applyInfoBean.getIdcardNationUrl());
        updateEntity.setBusinessLicenseArray(applyInfoBean.getBusinessLicenseArray());
        updateEntity.setStoreVideoUrl(applyInfoBean.getStoreVideoUrl());
        updateEntity.setCompanyName(applyInfoBean.getCompanyName());
        updateEntity.setCompanyAddress(applyInfoBean.getCompanyAddress());
        updateEntity.setCompanyPerson(applyInfoBean.getCompanyPerson());
        updateEntity.setCompanyPhone(applyInfoBean.getCompanyPhone());
        updateEntity.setCompanyLogo(applyInfoBean.getCompanyLogo());
        updateEntity.setOtherInfo(applyInfoBean.getOtherInfo());
//        updateEntity.setOwnerId(Integer.valueOf(applyInfoBean.getOwnerId()));
//        updateEntity.setOwnerName(applyInfoBean.getOwnerName());
        boolean b = this.updateById(updateEntity);

        return new ResponseVo(AppRspCodeConstant.SUCCESS);
    }

    @Override
    public ResponseVo getOssInfo(String randomCode) {

        MerchantApplyInfo merchantApplyInfo = this.baseMapper.getEntityByToken(randomCode);
        if (ObjectUtil.isEmpty(merchantApplyInfo)){
            return new ResponseVo(AppRspCodeConstant.SYS_EXCEPTION, "登陆凭证错误");
        }

        // 获取 商务信息
        JSONObject returnObject = new JSONObject();

        OssInfoVO ossInfo = storageService.getOssInfo(BizTypeEnum.MERCHANT.getType());
        // 获取oss 信息
        returnObject.put("ossInfo", ossInfo);

        return new ResponseVo(AppRspCodeConstant.SUCCESS, returnObject);
    }

    @Override
    public ResponseVo getApplyInfo(String randomCode) {

        MerchantApplyInfo merchantApplyInfo = this.baseMapper.getEntityByToken(randomCode);
        if (ObjectUtil.isEmpty(merchantApplyInfo)){
            return new ResponseVo(AppRspCodeConstant.SYS_EXCEPTION, "登陆凭证错误");
        }

        JSONObject returnObject = new JSONObject();

        ApplyInfoBean applyInfoBean = new ApplyInfoBean();
        applyInfoBean.setMobile(merchantApplyInfo.getMobile());
        applyInfoBean.setRealname(ObjectUtil.isEmpty(merchantApplyInfo.getRealname())?"":merchantApplyInfo.getRealname());
        applyInfoBean.setIdcardHeadUrl(ObjectUtil.isEmpty(merchantApplyInfo.getIdcardHeadUrl())?"":merchantApplyInfo.getIdcardHeadUrl());
        applyInfoBean.setIdcardNationUrl(ObjectUtil.isEmpty(merchantApplyInfo.getIdcardNationUrl())?"":merchantApplyInfo.getIdcardNationUrl());
        applyInfoBean.setBusinessLicenseArray(ObjectUtil.isEmpty(merchantApplyInfo.getBusinessLicenseArray())?"":merchantApplyInfo.getBusinessLicenseArray());
        applyInfoBean.setStoreVideoUrl(ObjectUtil.isEmpty(merchantApplyInfo.getStoreVideoUrl())?"":merchantApplyInfo.getStoreVideoUrl());

        applyInfoBean.setCompanyName(ObjectUtil.isEmpty(merchantApplyInfo.getCompanyName())?"":merchantApplyInfo.getCompanyName());
        applyInfoBean.setCompanyAddress(ObjectUtil.isEmpty(merchantApplyInfo.getCompanyAddress())?"":merchantApplyInfo.getCompanyAddress());
        applyInfoBean.setCompanyPerson(ObjectUtil.isEmpty(merchantApplyInfo.getCompanyPerson())?"":merchantApplyInfo.getCompanyPerson());
        applyInfoBean.setCompanyPhone(ObjectUtil.isEmpty(merchantApplyInfo.getCompanyPhone())?"":merchantApplyInfo.getCompanyPhone());
        applyInfoBean.setCompanyLogo(ObjectUtil.isEmpty(merchantApplyInfo.getCompanyLogo())?"":merchantApplyInfo.getCompanyLogo());
        applyInfoBean.setOtherInfo(ObjectUtil.isEmpty(merchantApplyInfo.getOtherInfo())?"":merchantApplyInfo.getOtherInfo());

        applyInfoBean.setStatus(merchantApplyInfo.getStatus());
        applyInfoBean.setTipsMessage(merchantApplyInfo.getTipsMsg());

        applyInfoBean.setOwnerId(ObjectUtil.isEmpty(merchantApplyInfo.getOwnerId())?"":merchantApplyInfo.getOwnerId()+"");
        applyInfoBean.setOwnerName(ObjectUtil.isEmpty(merchantApplyInfo.getOwnerName())?"":merchantApplyInfo.getOwnerName());

        returnObject.put("applyInfo", applyInfoBean);

        return new ResponseVo(AppRspCodeConstant.SUCCESS, returnObject);
    }


    public boolean saveApplyInfo(ApplyInfoBean applyInfoBean){
        MerchantApplyInfo merchantApplyInfo = new MerchantApplyInfo();
        merchantApplyInfo.setMobile(applyInfoBean.getMobile());
        merchantApplyInfo.setRealname(applyInfoBean.getRealname());
        merchantApplyInfo.setIdcardHeadUrl(applyInfoBean.getIdcardHeadUrl());
        merchantApplyInfo.setIdcardNationUrl(applyInfoBean.getIdcardNationUrl());
        merchantApplyInfo.setBusinessLicenseArray(applyInfoBean.getBusinessLicenseArray());
        merchantApplyInfo.setStoreVideoUrl(applyInfoBean.getStoreVideoUrl());
        merchantApplyInfo.setCompanyName(applyInfoBean.getCompanyName());
        merchantApplyInfo.setCompanyAddress(applyInfoBean.getCompanyAddress());
        merchantApplyInfo.setCompanyPerson(applyInfoBean.getCompanyPerson());
        merchantApplyInfo.setCompanyPhone(applyInfoBean.getCompanyPhone());
        merchantApplyInfo.setCompanyLogo(applyInfoBean.getCompanyLogo());
        merchantApplyInfo.setOtherInfo(applyInfoBean.getOtherInfo());
        merchantApplyInfo.setOwnerId(Integer.valueOf(applyInfoBean.getOwnerId()));
        merchantApplyInfo.setOwnerName(applyInfoBean.getOwnerName());

        boolean save = this.save(merchantApplyInfo);
        return save;
    }



}
